<template>
  <div>
    <detail-nav-bar :currentIndex="currentIndex" @navBarClick="navBarClick"/>
    <scroll class="content"
            ref="scroll"
            @scroll="contentScroll"
            :probe-type="3"
            :pullUpLoad="true">
      <detail-swiper :detailsImgs="topImages"></detail-swiper>
      <good-describe :goodsDscb="goods"></good-describe>
      <shop-describe :shopDscb="shop"></shop-describe>
      <deatil-goods-info :detailInfo="detailInfo" @imgLoad="imageLoad"/>
      <detail-param-info ref="param" :param-info="paramInfo" />
      <detail-comment-info ref="comment" :comment-info="commentInfo"/>
      <goods-list ref="recommend" :goods="goodsList"/>
    </scroll>
    <back-top v-show="showBackTop" @click.native="backTop"/>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>

  </div>
</template>

<script>
  import DetailNavBar from "./childComps/DetailNavBar";
  import Swiper from "components/common/swiper/Swiper";
  import {getDetail, getRecommend, Goods, Shop, GoodsParam} from "network/detail";
  import DetailSwiper from "./childComps/DetailSwiper";
  import Scroll from "components/common/scroll/Scroll";
  import GoodDescribe from "./childComps/GoodDescribe";
  import ShopDescribe from "./childComps/ShopDescribe";
  import DeatilGoodsInfo from "./childComps/DeatilGoodsInfo";
  import DetailParamInfo from "./childComps/DetailParamInfo";
  import DetailCommentInfo from "./childComps/DetailCommentInfo";
  import GoodsList from 'components/content/goods/GoodsList'
  import DetailBottomBar from "./childComps/DetailBottomBar";

  import {backTopMixin} from 'common/mixin'
  import {mapActions} from'vuex'


  export default {
    name: "Detail",
    mixins:[backTopMixin],
    components: {
      DetailNavBar,
      Swiper,
      DetailSwiper,
      Scroll,
      GoodDescribe,
      ShopDescribe,
      DeatilGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      GoodsList,
      DetailBottomBar
    },
    data(){
      return{
        iid: '',
        topImages: [],
        goods:{},
        shop:{},
        detailInfo:{},
        paramInfo:{},
        commentInfo:{},
        goodsList:[],
        themeTops: [],
        currentIndex:0
      }
    },
    created() {
      this.iid = this.$route.query.iid
      this._getDetail(this.iid)
      this._getRecommend()
    },
    methods: {
      ...mapActions({
        addCart:"addToCart"
      }),
      addToCart() {
        // 2.将商品信息添加到Store中
        const obj = {}
        obj.iid = this.iid
        obj.imgURL = this.topImages[0]
        obj.title = this.goods.title
        obj.price = this.goods.realPrice
        obj.desc = this.goods.desc
        // this.$store.dispatch('addToCart', obj).then(() => {
        //  this.$toast({message: '加入购物车成功'})
        // })
        this.addCart(obj)
      },
      navBarClick(index){
        this.$refs.scroll.scrollTo(0,-this.themeTops[index]-1)
      },
      imageLoad(){
        this.$refs.scroll.refresh()
        this.themeTops = [0]
        this.themeTops.push(this.$refs.param.$el.offsetTop)
        this.themeTops.push(this.$refs.comment.$el.offsetTop)
        this.themeTops.push(this.$refs.recommend.$el.offsetTop)
        this.themeTops.push(Number.MAX_VALUE)
      },
      contentScroll(position) {
        // 决定backTop按钮是否显示
        this.showBackTop = position.y <= -1000
        this.judgeDetaiScroll(-position.y)
      },
      judgeDetaiScroll(position){
        for(let i=1; i<this.themeTops.length; i++){
          if(position < this.themeTops[i]){
            this.currentIndex = i-1
            break
          }
        }
      },
      _getDetail(iid) {
        getDetail(iid).then(res => {
          // 1.获取数据
          const data = res.result

          // 2.获取顶部的图片数据
          this.topImages = data.itemInfo.topImages

          // 3.获取商品信息
          this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

          // 4.获取店铺信息
          this.shop = new Shop(data.shopInfo)

          // 5.获取商品详细信息
          this.detailInfo = data.detailInfo

          // 6.保存参数信息
          this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

          // 7.保存评论数据
          if (data.rate.list) {
            this.commentInfo = data.rate.list[0];
          }
        })
      },
      _getRecommend() {
        getRecommend().then(res => {
          this.goodsList = res.data.list
        })
      }
    }
  }
</script>

<style scoped>
  .content{
    position: absolute;
    top: 44px;
    bottom: 58px;
    left: 0;
    right: 0;
  }
</style>